package com.ruoyi.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.vo.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.SysMenu;
import com.ruoyi.domain.SysRole;
import com.ruoyi.domain.SysUser;
import com.ruoyi.mapper.SysMenuMapper;
import com.ruoyi.mapper.SysRoleMapper;

@Service
public class SysMenuService {
	
	@Autowired
	private SysMenuMapper sysMenuMapper;
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	public List<SysMenu> selectMenuList(Long userId) {
		return selectMenuList(new SysMenu(), userId);
	}
	
	public List<SysMenu> selectMenuList(SysMenu menu, Long userId) {
		List<SysMenu> menuList = null;
		if (SysUser.isAdmin(userId)) {
			menuList = sysMenuMapper.selectMenuList(menu);
		} else {
			menu.getParams().put("userId", userId);
			menuList = sysMenuMapper.selectMenuListByUserId(menu);
		}
		return menuList;
	}

	public SysMenu selectMenuById(Long menuId) {
		return sysMenuMapper.selectMenuById(menuId);
	}

	public boolean checkMenuNameUnique(SysMenu menu) {
		Long menuId = StringUtils.isNull(menu.getMenuId()) ? -1L : menu.getMenuId();
        SysMenu info = sysMenuMapper.checkMenuNameUnique(menu.getMenuName(), menu.getParentId());
        if (StringUtils.isNotNull(info) && info.getMenuId().longValue() != menuId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
	}

	public int insertMenu(SysMenu menu) {
		return sysMenuMapper.insertMenu(menu);
	}

	public int updateMenu(SysMenu menu) {
		return sysMenuMapper.updateMenu(menu);
	}

	public int deleteMenuById(Long menuId) {
		return sysMenuMapper.deleteMenuById(menuId);
	}

	public boolean hasChildByMenuId(Long menuId) {
		int a = sysMenuMapper.hasChildByMenuId(menuId);
		return a > 0;
	}

	public List<Long> selectMenuListByRoleId(Long roleId) {
		SysRole role  = sysRoleMapper.selectRoleById(roleId);
		return sysMenuMapper.selectMenuListByRoleId(roleId,role.isMenuCheckStrictly());
	}

	public List<TreeSelect> buildMenuTreeSelect(List<SysMenu> menus) {
		List<SysMenu> menuTrees = buildMenuTree(menus);
		return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
	}

	public List<SysMenu> buildMenuTree(List<SysMenu> menus) {
		List<SysMenu> returnList = new ArrayList<SysMenu>();
        List<Long> tempList = menus.stream().map(SysMenu::getMenuId).collect(Collectors.toList());
        for (Iterator<SysMenu> iterator = menus.iterator(); iterator.hasNext();) {
            SysMenu menu = (SysMenu) iterator.next();
            if (!tempList.contains(menu.getParentId())) {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        } if (returnList.isEmpty()) {
            returnList = menus;
        }
        return returnList;
	}

	private void recursionFn(List<SysMenu> list, SysMenu m) {
		List<SysMenu> childList = getChildList(list, m);
        m.setChildren(childList);
        for (SysMenu mChild : childList) {
            if (hasChild(list, mChild)) {
                recursionFn(list, mChild);
            }
        }
		
	}

	private boolean hasChild(List<SysMenu> list, SysMenu m) {
		return getChildList(list, m).size() > 0;
	}

	private List<SysMenu> getChildList(List<SysMenu> list, SysMenu m) {
		List<SysMenu> mlist = new ArrayList<SysMenu>();
        Iterator<SysMenu> it = list.iterator();
        while (it.hasNext()) {
            SysMenu n = (SysMenu) it.next();
            if (n.getParentId().longValue() == m.getMenuId().longValue()) {
                mlist.add(n);
            }
        }
        return mlist;
	}

	public Set<String> selectMenuPermsByRoleId(Long roleId) {
        List<String> perms = sysMenuMapper.selectMenuPermsByRoleId(roleId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms) {
            if (StringUtils.isNotEmpty(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

	public Set<String> selectMenuPermsByUserId(Long userId) {
        List<String> perms = sysMenuMapper.selectMenuPermsByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms)  {
            if (StringUtils.isNotEmpty(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

	
	
	
	
}
